package com.farm.wcp.task;

import com.farm.authority.domain.User;
import com.farm.doc.server.FarmDocOperateRightInter;
import com.farm.util.spring.BeanFactory;
import com.farm.wcp.know.service.KnowServiceInter;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FilenameFilter;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by william on 2019/1/5.
 * 服务器上“南方日报”分类id：ff80818167ea982e0167eaad3f000038
 * 爬虫工程路径：E:\project\tutorial\scrapy crawl southcn
 * 爬取文件路径：E:\project\tutorial\southcn\20190106
 */
public class PullTask_GZRB implements Job {

	public static void main(String[] args) throws JobExecutionException {
		PullTask_GZRB pn = new PullTask_GZRB();
		pn.execute(null);
	}
    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {

        //python scrapy crawl quotes
        String docgroup="0";
        String knowtype="ff80818167ea982e0167eaad703b0039";
        String writetype="1";
        String readtype="1";

        String knowtitle="";
        String text="";
        String knowtag="";

        User user = new User();
        user.setId("ff80818167ea982e01680ec2545002b7");
        user.setName("智能爬虫");

        KnowServiceInter KnowServiceImpl = (KnowServiceInter) BeanFactory.getBean("knowServiceImpl");
        try {
//            Process process = Runtime.getRuntime().exec("E:\\project\\tutorial\\scrapy crawl southcn");
            //Process process = Runtime.getRuntime().exec("cmd");
            Date now = new Date( );
            SimpleDateFormat ft = new SimpleDateFormat("yyyyMMdd");
            //读取目录下文件
            File dir = new File("/home/tj03/install/getdata/gzdaily/"+ft.format(now));
            File[] dirs = dir.listFiles(new FilenameFilter() {
                @Override
                public boolean accept(File dir, String name) {
                    return !name.equals("img");
                }
            });
            for(File cdir:dirs){
                File[] files = cdir.listFiles();
                for(File file:files){
                    System.out.println("jobExecutionContext = [" + file.getName() + "]");
                    if ("0".equals(docgroup)) {
                        docgroup = null;
                    }
                    knowtitle = file.getName().replace(".html","");
                    BufferedReader br = new BufferedReader(new FileReader(file));//构造一个BufferedReader类来读取文件
                    String s = null;
                    StringBuilder result = new StringBuilder();
                    while((s = br.readLine())!=null){//使用readLine方法，一次读一行
                        result.append(System.lineSeparator()+s);
                    }
                    br.close();
                    text = result.toString();
                    KnowServiceImpl.creatKnow(knowtitle, knowtype, text, knowtag, FarmDocOperateRightInter.POP_TYPE.getEnum(writetype),
                            FarmDocOperateRightInter.POP_TYPE.getEnum(readtype), docgroup, user);
                }
            }


        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
